package com.wuhao.lib_base.utils;

import android.animation.Animator;
import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.airbnb.lottie.LottieAnimationView;
import com.blankj.utilcode.util.TimeUtils;
import com.wuhao.lib_base.R;
import com.wuhao.lib_base.weight.MyDialog;
import com.wuhao.lib_common.utils.NotNullHelper;

import java.util.concurrent.TimeUnit;

import io.reactivex.rxjava3.core.Observable;


/**
 * Created by Mr.Hao on 2017/11/1.
 *
 * @author zhangjianhao
 */

public class DialogUtil {

    private static MyDialog dialog;
    private static TextView loading_text;
    private static LottieAnimationView aniView;
    private static long startTime = 0L;
    private static long endTime = 0L;

    public static void showLoading(Context context, @Nullable String dialogName, boolean isVertical) {
        if (dialog == null) {
            View view;
            if (isVertical) {
                view = LayoutInflater.from(context).inflate(R.layout.loading_layout_vertical, null);
            } else {
                view = LayoutInflater.from(context).inflate(R.layout.loading_layout_horizontal, null);
            }

            dialog = new MyDialog(context, RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT, view, 0);
            loading_text = view.findViewById(R.id.loading_text);
            if (!NotNullHelper.notEmpty(dialogName)) {
                loading_text.setText("正在加载...");
            } else {
                loading_text.setText(dialogName);
            }
            dialog.show();
            startTime = TimeUtils.getNowMills();
            dialog.setCanceledOnTouchOutside(false);
            dialog.setCancelable(false);
        }
    }

    /**
     * 通过动画控制加载dialog
     *
     * @param context
     * @param dialogName
     */
    public static void showAniLoading(Context context, @Nullable String dialogName) {
        if (dialog == null) {
            View view = LayoutInflater.from(context).inflate(R.layout.base_loading_layout, null);
            dialog = new MyDialog(context, RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT, view, 0);
            loading_text = view.findViewById(R.id.loading_text);
            if (TextUtils.isEmpty(dialogName)) {
                loading_text.setText("正在加载...");
            } else {
                loading_text.setText(dialogName);
            }
            aniView = view.findViewById(R.id.aniView);
            aniView.addAnimatorListener(new Animator.AnimatorListener() {
                @Override
                public void onAnimationStart(Animator animation) {
                }

                @Override
                public void onAnimationEnd(Animator animation) {
                    dismissDialog(context);
                }

                @Override
                public void onAnimationCancel(Animator animation) {
                }

                @Override
                public void onAnimationRepeat(Animator animation) {
                }
            });
            dialog.show();
            dialog.setCanceledOnTouchOutside(false);
            dialog.setCancelable(false);
        }
    }

    public static void updateLoadingName(String updateName) {
        if (dialog != null) {
            if (loading_text != null) {
                loading_text.setText(updateName);
            }
        }
    }

    public static void dismissDialog(Context context) {
        if (dialog != null) {
            endTime = TimeUtils.getNowMills();
            long distance = endTime - startTime;
            //判断是否显示时间大于500毫秒
            if (distance < 500) {
                //显示不足500毫秒
                //显示够500毫秒之后在进行dialog的dissmiss
                long timer = 500 - distance;
                Observable.timer(timer, TimeUnit.MILLISECONDS)
                        .subscribe(aLong -> {
                            if (dialog!=null) {
                                dialog.dismiss();
                                dialog = null;
                            }
                        });
            } else {
                if (dialog!=null) {
                    dialog.dismiss();
                    dialog = null;
                }
            }
        }
    }
}
